From ecb47bfb8f729dd26c0d47b0ddae7761f32dda99 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 9 Mar 2016 16:47:58 +0000 Subject: [PATCH] phpunit: Abstract user-lang override in MediaWikiTestCase Removed redundant set up in these classes (same as their paren class MediaWikiLangTestCase does already). * BlockTest * ExportTest * MWTimestampTest * TitlePermissionTest Change-Id: I28d18cb797bb249981727b02dffce4f0d8682b02 --- tests/phpunit/MediaWikiLangTestCase.php | 16 +++-------- tests/phpunit/MediaWikiTestCase.php | 27 +++++++++++++++++++ tests/phpunit/includes/BlockTest.php | 8 ------ tests/phpunit/includes/ExportTest.php | 2 -- tests/phpunit/includes/ExtraParserTest.php | 6 ++--- tests/phpunit/includes/HtmlTest.php | 16 +++++------ tests/phpunit/includes/MWTimestampTest.php | 2 -- tests/phpunit/includes/MessageTest.php | 6 ++--- .../phpunit/includes/TitlePermissionTest.php | 4 --- tests/phpunit/includes/TitleTest.php | 11 +++----- .../includes/cache/MessageCacheTest.php | 10 ++----- .../includes/changes/OldChangesListTest.php | 4 +-- .../changes/TestRecentChangesHelper.php | 2 +- .../includes/exception/MWExceptionTest.php | 10 +++---- .../includes/page/ArticleTablesTest.php | 7 +++-- .../title/MediaWikiTitleCodecTest.php | 6 ++--- 16 files changed, 63 insertions(+), 74 deletions(-) diff --git a/tests/phpunit/MediaWikiLangTestCase.php b/tests/phpunit/MediaWikiLangTestCase.php index 214bcc11ba..fd308b2d95 100644 --- a/tests/phpunit/MediaWikiLangTestCase.php +++ b/tests/phpunit/MediaWikiLangTestCase.php @@ -6,7 +6,6 @@ abstract class MediaWikiLangTestCase extends MediaWikiTestCase { protected function setUp() { global $wgLanguageCode, $wgContLang; - parent::setUp(); if ( $wgLanguageCode != $wgContLang->getCode() ) { throw new MWException( "Error in MediaWikiLangTestCase::setUp(): " . @@ -14,18 +13,11 @@ abstract class MediaWikiLangTestCase extends MediaWikiTestCase { "\$wgContLang->getCode() (" . $wgContLang->getCode() . ")" ); } - // HACK: Call getLanguage() so the real $wgContLang is cached as the user language - // rather than our fake one. This is to avoid breaking other, unrelated tests. - RequestContext::getMain()->getLanguage(); - - $langCode = 'en'; # For mainpage to be 'Main Page' - $langObj = Language::factory( $langCode ); + parent::setUp(); - $this->setMwGlobals( [ - 'wgLanguageCode' => $langCode, - 'wgLang' => $langObj, - 'wgContLang' => $langObj, - ] ); + $this->setUserLang( 'en' ); + // For mainpage to be 'Main Page' + $this->setContentLang( 'en' ); MessageCache::singleton()->disable(); } diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index d5192ace38..3bd5ed852b 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -415,6 +415,33 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { $this->setMwGlobals( $name, $merged ); } + /** + * @since 1.27 + * @param string|Language $lang + */ + public function setUserLang( $lang ) { + RequestContext::getMain()->setLanguage( $lang ); + $this->setMwGlobals( 'wgLang', RequestContext::getMain()->getLanguage() ); + } + + /** + * @since 1.27 + * @param string|Language $lang + */ + public function setContentLang( $lang ) { + if ( $lang instanceof Language ) { + $langCode = $lang->getCode(); + $langObj = $lang; + } else { + $langCode = $lang; + $langObj = Language::factory( $langCode ); + } + $this->setMwGlobals( [ + 'wgLanguageCode' => $langCode, + 'wgContLang' => $langObj, + ] ); + } + /** * Sets the logger for a specified channel, for the duration of the test. * @since 1.27 diff --git a/tests/phpunit/includes/BlockTest.php b/tests/phpunit/includes/BlockTest.php index 28192e9810..e14960ab03 100644 --- a/tests/phpunit/includes/BlockTest.php +++ b/tests/phpunit/includes/BlockTest.php @@ -13,14 +13,6 @@ class BlockTest extends MediaWikiLangTestCase { /* variable used to save up the blockID we insert in this test suite */ private $blockId; - protected function setUp() { - parent::setUp(); - $this->setMwGlobals( [ - 'wgLanguageCode' => 'en', - 'wgContLang' => Language::factory( 'en' ) - ] ); - } - function addDBData() { $user = User::newFromName( 'UTBlockee' ); diff --git a/tests/phpunit/includes/ExportTest.php b/tests/phpunit/includes/ExportTest.php index d451e2100d..a2bb97a2bc 100644 --- a/tests/phpunit/includes/ExportTest.php +++ b/tests/phpunit/includes/ExportTest.php @@ -12,8 +12,6 @@ class ExportTest extends MediaWikiLangTestCase { protected function setUp() { parent::setUp(); $this->setMwGlobals( [ - 'wgContLang' => Language::factory( 'en' ), - 'wgLanguageCode' => 'en', 'wgCapitalLinks' => true, ] ); } diff --git a/tests/phpunit/includes/ExtraParserTest.php b/tests/phpunit/includes/ExtraParserTest.php index 78e71ea467..5dd4f7a07c 100644 --- a/tests/phpunit/includes/ExtraParserTest.php +++ b/tests/phpunit/includes/ExtraParserTest.php @@ -18,12 +18,12 @@ class ExtraParserTest extends MediaWikiTestCase { $contLang = Language::factory( 'en' ); $this->setMwGlobals( [ 'wgShowDBErrorBacktrace' => true, - 'wgLanguageCode' => 'en', - 'wgContLang' => $contLang, - 'wgLang' => Language::factory( 'en' ), 'wgCleanSignatures' => true, ] ); + $this->setUserLang( 'en' ); + $this->setContentLang( $contLang ); + // FIXME: This test should pass without setting global content language $this->options = ParserOptions::newFromUserAndLang( new User, $contLang ); $this->options->setTemplateCallback( [ __CLASS__, 'statelessFetchTemplate' ] ); $this->parser = new Parser; diff --git a/tests/phpunit/includes/HtmlTest.php b/tests/phpunit/includes/HtmlTest.php index a18e3c8c76..a90295a4c1 100644 --- a/tests/phpunit/includes/HtmlTest.php +++ b/tests/phpunit/includes/HtmlTest.php @@ -6,8 +6,11 @@ class HtmlTest extends MediaWikiTestCase { protected function setUp() { parent::setUp(); - $langCode = 'en'; - $langObj = Language::factory( $langCode ); + $this->setMwGlobals( [ + 'wgWellFormedXml' => false, + ] ); + + $langObj = Language::factory( 'en' ); // Hardcode namespaces during test runs, // so that html output based on existing namespaces @@ -32,13 +35,8 @@ class HtmlTest extends MediaWikiTestCase { 100 => 'Custom', 101 => 'Custom_talk', ] ); - - $this->setMwGlobals( [ - 'wgLanguageCode' => $langCode, - 'wgContLang' => $langObj, - 'wgLang' => $langObj, - 'wgWellFormedXml' => false, - ] ); + $this->setUserLang( $langObj ); + $this->setContentLang( $langObj ); } /** diff --git a/tests/phpunit/includes/MWTimestampTest.php b/tests/phpunit/includes/MWTimestampTest.php index 908376ba88..bca39824b7 100644 --- a/tests/phpunit/includes/MWTimestampTest.php +++ b/tests/phpunit/includes/MWTimestampTest.php @@ -10,8 +10,6 @@ class MWTimestampTest extends MediaWikiLangTestCase { // Avoid 'GetHumanTimestamp' hook and others $this->setMwGlobals( 'wgHooks', [] ); - - RequestContext::getMain()->setLanguage( Language::factory( 'en' ) ); } /** diff --git a/tests/phpunit/includes/MessageTest.php b/tests/phpunit/includes/MessageTest.php index bdd80e4c94..cf34b18713 100644 --- a/tests/phpunit/includes/MessageTest.php +++ b/tests/phpunit/includes/MessageTest.php @@ -6,9 +6,9 @@ class MessageTest extends MediaWikiLangTestCase { parent::setUp(); $this->setMwGlobals( [ - 'wgLang' => Language::factory( 'en' ), 'wgForceUIMsgAsContentMsg' => [], ] ); + $this->setUserLang( 'en' ); } /** @@ -517,7 +517,7 @@ class MessageTest extends MediaWikiLangTestCase { * @covers Message::inContentLanguage */ public function testInContentLanguage() { - $this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) ); + $this->setUserLang( 'fr' ); // NOTE: make sure internal caching of the message text is reset appropriately $msg = wfMessage( 'mainpage' ); @@ -531,9 +531,9 @@ class MessageTest extends MediaWikiLangTestCase { */ public function testInContentLanguageOverride() { $this->setMwGlobals( [ - 'wgLang' => Language::factory( 'fr' ), 'wgForceUIMsgAsContentMsg' => [ 'mainpage' ], ] ); + $this->setUserLang( 'fr' ); // NOTE: make sure internal caching of the message text is reset appropriately. // NOTE: wgForceUIMsgAsContentMsg forces the messages *current* language to be used. diff --git a/tests/phpunit/includes/TitlePermissionTest.php b/tests/phpunit/includes/TitlePermissionTest.php index a4d0059631..584a368b36 100644 --- a/tests/phpunit/includes/TitlePermissionTest.php +++ b/tests/phpunit/includes/TitlePermissionTest.php @@ -26,14 +26,10 @@ class TitlePermissionTest extends MediaWikiLangTestCase { protected function setUp() { parent::setUp(); - $langObj = Language::factory( 'en' ); $localZone = 'UTC'; $localOffset = date( 'Z' ) / 60; $this->setMwGlobals( [ - 'wgContLang' => $langObj, - 'wgLanguageCode' => 'en', - 'wgLang' => $langObj, 'wgLocaltimezone' => $localZone, 'wgLocalTZoffset' => $localOffset, 'wgNamespaceProtection' => [ diff --git a/tests/phpunit/includes/TitleTest.php b/tests/phpunit/includes/TitleTest.php index f2ad1c6172..b3465e1a85 100644 --- a/tests/phpunit/includes/TitleTest.php +++ b/tests/phpunit/includes/TitleTest.php @@ -9,14 +9,12 @@ class TitleTest extends MediaWikiTestCase { parent::setUp(); $this->setMwGlobals( [ - 'wgLanguageCode' => 'en', - 'wgContLang' => Language::factory( 'en' ), - // User language - 'wgLang' => Language::factory( 'en' ), 'wgAllowUserJs' => false, 'wgDefaultLanguageVariant' => false, 'wgMetaNamespace' => 'Project', ] ); + $this->setUserLang( 'en' ); + $this->setContentLang( 'en' ); } /** @@ -421,12 +419,11 @@ class TitleTest extends MediaWikiTestCase { ) { // Setup environnement for this test $this->setMwGlobals( [ - 'wgLanguageCode' => $contLang, - 'wgContLang' => Language::factory( $contLang ), - 'wgLang' => Language::factory( $lang ), 'wgDefaultLanguageVariant' => $variant, 'wgAllowUserJs' => true, ] ); + $this->setUserLang( $lang ); + $this->setContentLang( $contLang ); $title = Title::newFromText( $titleText ); $this->assertInstanceOf( 'Title', $title, diff --git a/tests/phpunit/includes/cache/MessageCacheTest.php b/tests/phpunit/includes/cache/MessageCacheTest.php index ddd74e3919..38cc863a4b 100644 --- a/tests/phpunit/includes/cache/MessageCacheTest.php +++ b/tests/phpunit/includes/cache/MessageCacheTest.php @@ -19,14 +19,8 @@ class MessageCacheTest extends MediaWikiLangTestCase { protected function configureLanguages() { // for the test, we need the content language to be anything but English, // let's choose e.g. German (de) - $langCode = 'de'; - $langObj = Language::factory( $langCode ); - - $this->setMwGlobals( [ - 'wgLanguageCode' => $langCode, - 'wgLang' => $langObj, - 'wgContLang' => $langObj, - ] ); + $this->setUserLang( 'de' ); + $this->setContentLang( 'de' ); } function addDBData() { diff --git a/tests/phpunit/includes/changes/OldChangesListTest.php b/tests/phpunit/includes/changes/OldChangesListTest.php index 79cc666ab6..5746a61dc4 100644 --- a/tests/phpunit/includes/changes/OldChangesListTest.php +++ b/tests/phpunit/includes/changes/OldChangesListTest.php @@ -29,8 +29,8 @@ class OldChangesListTest extends MediaWikiLangTestCase { $this->setMwGlobals( [ 'wgArticlePath' => '/wiki/$1', - 'wgLang' => Language::factory( 'qqx' ) ] ); + $this->setUserLang( 'qqx' ); } /** @@ -197,7 +197,7 @@ class OldChangesListTest extends MediaWikiLangTestCase { private function getContext() { $user = $this->getTestUser(); $context = $this->testRecentChangesHelper->getTestContext( $user ); - $context->setLanguage( Language::factory( 'qqx' ) ); + $context->setLanguage( 'qqx' ); return $context; } diff --git a/tests/phpunit/includes/changes/TestRecentChangesHelper.php b/tests/phpunit/includes/changes/TestRecentChangesHelper.php index 128e067f9a..faa1dccd01 100644 --- a/tests/phpunit/includes/changes/TestRecentChangesHelper.php +++ b/tests/phpunit/includes/changes/TestRecentChangesHelper.php @@ -154,7 +154,7 @@ class TestRecentChangesHelper { public function getTestContext( User $user ) { $context = new RequestContext(); - $context->setLanguage( Language::factory( 'en' ) ); + $context->setLanguage( 'en' ); $context->setUser( $user ); diff --git a/tests/phpunit/includes/exception/MWExceptionTest.php b/tests/phpunit/includes/exception/MWExceptionTest.php index 94c039c2df..0e87ffa42f 100644 --- a/tests/phpunit/includes/exception/MWExceptionTest.php +++ b/tests/phpunit/includes/exception/MWExceptionTest.php @@ -19,9 +19,9 @@ class MWExceptionTest extends MediaWikiTestCase { * @dataProvider provideTextUseOutputPage * @covers MWException::useOutputPage */ - public function testUseOutputPage( $expected, $wgLang, $wgFullyInitialised, $wgOut ) { + public function testUseOutputPage( $expected, $langObj, $wgFullyInitialised, $wgOut ) { $this->setMwGlobals( [ - 'wgLang' => $wgLang, + 'wgLang' => $langObj, 'wgFullyInitialised' => $wgFullyInitialised, 'wgOut' => $wgOut, ] ); @@ -32,7 +32,7 @@ class MWExceptionTest extends MediaWikiTestCase { public function provideTextUseOutputPage() { return [ - // expected, wgLang, wgFullyInitialised, wgOut + // expected, langObj, wgFullyInitialised, wgOut [ false, null, null, null ], [ false, $this->getMockLanguage(), null, null ], [ false, $this->getMockLanguage(), true, null ], @@ -52,9 +52,9 @@ class MWExceptionTest extends MediaWikiTestCase { * @dataProvider provideUseMessageCache * @covers MWException::useMessageCache */ - public function testUseMessageCache( $expected, $wgLang ) { + public function testUseMessageCache( $expected, $langObj ) { $this->setMwGlobals( [ - 'wgLang' => $wgLang, + 'wgLang' => $langObj, ] ); $e = new MWException(); $this->assertEquals( $expected, $e->useMessageCache() ); diff --git a/tests/phpunit/includes/page/ArticleTablesTest.php b/tests/phpunit/includes/page/ArticleTablesTest.php index d88592600f..3a3b514651 100644 --- a/tests/phpunit/includes/page/ArticleTablesTest.php +++ b/tests/phpunit/includes/page/ArticleTablesTest.php @@ -17,9 +17,8 @@ class ArticleTablesTest extends MediaWikiLangTestCase { $page = WikiPage::factory( $title ); $user = new User(); $user->mRights = [ 'createpage', 'edit', 'purge' ]; - $this->setMwGlobals( 'wgLanguageCode', 'es' ); - $this->setMwGlobals( 'wgContLang', Language::factory( 'es' ) ); - $this->setMwGlobals( 'wgLang', Language::factory( 'fr' ) ); + $this->setContentLang( 'es' ); + $this->setUserLang( 'fr' ); $page->doEditContent( new WikitextContent( '{{:{{int:history}}}}' ), @@ -30,7 +29,7 @@ class ArticleTablesTest extends MediaWikiLangTestCase { ); $templates1 = $title->getTemplateLinksFrom(); - $this->setMwGlobals( 'wgLang', Language::factory( 'de' ) ); + $this->setUserLang( 'de' ); $page = WikiPage::factory( $title ); // In order to force the re-rendering of the same wikitext // We need an edit, a purge is not enough to regenerate the tables diff --git a/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php b/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php index 1ac5e5992d..0bb0afeec0 100644 --- a/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php +++ b/tests/phpunit/includes/title/MediaWikiTitleCodecTest.php @@ -32,10 +32,6 @@ class MediaWikiTitleCodecTest extends MediaWikiTestCase { parent::setUp(); $this->setMwGlobals( [ - 'wgLanguageCode' => 'en', - 'wgContLang' => Language::factory( 'en' ), - // User language - 'wgLang' => Language::factory( 'en' ), 'wgAllowUserJs' => false, 'wgDefaultLanguageVariant' => false, 'wgMetaNamespace' => 'Project', @@ -57,6 +53,8 @@ class MediaWikiTitleCodecTest extends MediaWikiTestCase { ] ] ] ); + $this->setUserLang( 'en' ); + $this->setContentLang( 'en' ); } /** -- 2.20.1